package com.king.tv.mvp.presenter;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.king.base.util.LogUtils;
import com.king.base.util.StringUtils;
import com.king.tv.App;
import com.king.tv.bean.LiveInfo;
import com.king.tv.bean.LiveListResult;
import com.king.tv.bean.P;
import com.king.tv.bean.SearchRequestBody;
import com.king.tv.bean.SearchResult;
import com.king.tv.bean.Video;
import com.king.tv.http.BaseCallModel;
import com.king.tv.http.HttpLoader;
import com.king.tv.mvp.base.BasePresenter;
import com.king.tv.mvp.view.ILiveListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
 * @since 2017/2/21
 */

public class LiveListPresenter extends BasePresenter<ILiveListView> {

    @Inject
    public LiveListPresenter(App app) {
        super(app);
    }


    public void getLiveList(String slug,int type){
        if(type==2){
            getLiveListByType();
        }else{
            if(StringUtils.isBlank(slug)){
                getLiveList();
            }else{
                getLiveListBySlug(slug);
            }
        }
    }

    private void getLiveListByType() {
        if(isViewAttached()){
            getView().showProgress();
        }
        HttpLoader.getInstance().getMovie("","1").subscribe(new Observer<Object>() {
            @Override
            public void onSubscribe(Disposable d) {

            }
            @Override
            public void onNext(Object value) {
                try {
                    Gson gson = new Gson();
                    String json = gson.toJson(value);
                    Log.i("tag","========json"+json);
                    BaseCallModel baseCallModel = gson.fromJson(json,BaseCallModel.class);
                    Log.i("tag","========json"+baseCallModel.getData());
                    String result = gson.toJson(baseCallModel.getData());
                    if(baseCallModel!=null){
                        List<Video> persons =gson.fromJson(result,new TypeToken<List<Video>>() {}.getType());
                        //new TypeToken<List<Video>>() {}.getType()
                        if(isViewAttached()){
                            getView().onGetLiveList(persons);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable e) {
                Log.i("tag","==============frag"+e);
                if(isViewAttached()){
                    getView().onError(e);
                }
            }

            @Override
            public void onComplete() {
                if(isViewAttached()){
                    getView().onCompleted();
                }
            }
        });
    }


    public void getLiveList(){
        Log.i("tag","================livelist");
        if(isViewAttached()){
            getView().showProgress();
        }
        HttpLoader.getInstance().getHotM().subscribe(new Observer<Object>() {
            @Override
            public void onSubscribe(Disposable d) {

            }
            @Override
            public void onNext(Object value) {
                try {
                    Gson gson = new Gson();
                    String json = gson.toJson(value);
                    Log.i("tag","========json"+json);
                    BaseCallModel baseCallModel = gson.fromJson(json,BaseCallModel.class);
                    Log.i("tag","========json"+baseCallModel.getData());
                    String result = gson.toJson(baseCallModel.getData());
                    if(baseCallModel!=null){
                        List<Video> persons =gson.fromJson(result,new TypeToken<List<Video>>() {}.getType());
                        //new TypeToken<List<Video>>() {}.getType()
                        if(isViewAttached()){
                            getView().onGetLiveList(persons);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable e) {
                Log.i("tag","==============frag"+e);
                if(isViewAttached()){
                    getView().onError(e);
                }
            }

            @Override
            public void onComplete() {
                if(isViewAttached()){
                    getView().onCompleted();
                }
            }
        });
    }


    public void getLiveListBySlug(String slug){
        if(isViewAttached()){
            getView().showProgress();
        }
        Log.i("tag","=====fragment请求"+slug.toString());
        HttpLoader.getInstance().getMovie(slug,"0").subscribe(new Observer<Object>() {
            @Override
            public void onSubscribe(Disposable d) {

            }
            @Override
            public void onNext(Object value) {
                try {
                    Gson gson = new Gson();
                    String result = gson.toJson(value);
                    BaseCallModel baseCallModel = gson.fromJson(result,BaseCallModel.class);
                    if(baseCallModel!=null){
                        String json = gson.toJson(baseCallModel.getData());
                        List<Video> persons =gson.fromJson(json, new TypeToken<List<Video>>(){}.getType());
                        if(isViewAttached()){
                            getView().onGetLiveList(persons);
                        }
                    }
                } catch (Exception e) {
                    Log.i("tag","========json"+e);
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.i("tag","==============frag"+e);
                if(isViewAttached()){
                    getView().onError(e);
                }
            }

            @Override
            public void onComplete() {
                if(isViewAttached()){
                    getView().onCompleted();
                }
            }
        });
//        getAppComponent().getAPIService()
//                .getLiveListResultByCategories(slug)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<LiveListResult>() {
//                    @Override
//                    public void onError(Throwable e) {
//                        if(isViewAttached())
//                            getView().onError(e);
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        if(isViewAttached())
//                            getView().onCompleted();
//                    }
//
//                    @Override
//                    public void onSubscribe(Disposable d) {
//
//                    }
//
//                    @Override
//                    public void onNext(LiveListResult liveListResult) {
//                        LogUtils.d("Response:" + liveListResult);
//                        List<LiveInfo> list = null;
//                        if(liveListResult!=null){
//                            list = liveListResult.getData();
//                        }
//                        if(isViewAttached())
//                            getView().onGetLiveList(list);
//
//                    }
//                });
    }

    public void getLiveListByKey(String key,int page){
        getLiveListByKey(key,page,P.DEFAULT_SIZE);
    }

    public void getLiveListByKey(String key, final int page, int pageSize){
        if(isViewAttached()){
            getView().showProgress();
        }
//        getAppComponent().getAPIService()
//                .search(SearchRequestBody.getInstance(new P(page,key,pageSize)))
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .map(new Func1<SearchResult, List<LiveInfo>>() {
//
//                    @Override
//                    public List<LiveInfo> call(SearchResult searchResult) {
//                        LogUtils.d("Response:" + searchResult);
//                        if(searchResult!=null){
//                            if(searchResult.getData()!=null){
//                                return searchResult.getData().getItems();
//                            }else{
//                                LogUtils.d(searchResult.toString());
//                            }
//
//                        }
//                       return null;
//                    }
//                })
//                .onErrorReturn(new Func1<Throwable, List<LiveInfo>>() {
//                    @Override
//                    public List<LiveInfo> call(Throwable throwable) {
//                        LogUtils.w(throwable);
//                        return null;
//                    }
//                })
//                .subscribe(new Action1<List<LiveInfo>>() {
//                    @Override
//                    public void call(List<LiveInfo> list) {
//                        if(isViewAttached()){
//                            if(page>0){
//                                getView().onGetMoreLiveList(list);
//                            }else{
//                                getView().onGetLiveList(list);
//                            }
//
//                        }
//
//                    }
//                });

    }
}
